<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="设备编号" prop="deviceNo">
        <el-input
          v-model="queryParams.deviceNo"
          placeholder="请输入设备编号"
          clearable
          @Keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="setCycleFun"
          v-hasPermi="['base:device:edit']"
        >批量设置周期</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
        :columns="columns"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="jobDispatchList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="任务名称"
        align="center"
        prop="name"
        :show-overflow-tooltip="true"
        v-if="columns[0].visible"
      />
      <el-table-column
        label="设备名称"
        align="center"
        prop="device.deviceName"
        v-if="columns[1].visible"
      />
      <el-table-column
        label="设备编号"
        align="center"
        prop="deviceNo"
        v-if="columns[2].visible"
      />
      <el-table-column
        label="通道号"
        align="center"
        prop="channel"
        v-if="columns[3].visible"
      />
      <el-table-column
        label="cron执行表达式"
        align="center"
        prop="job.cronExpression"
        v-if="columns[4].visible"
      />
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.job.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="setCycleFun(scope.row)"
          >设置周期</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="alarmCycleFun(scope.row)"
          >报警阈值</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <dispatchDialog
      :dispatchVis="dispatchVis"
      :ediExpressionObj="ediExpressionObj"
      name="thresholdValue"
    ></dispatchDialog>
    <!-- 设置阈值 -->
    <settingThreshold
      :cycleVis="cycleVis"
      :cycleObj="cycleObj"
    ></settingThreshold>
  </div>
</template>

<script>
import { listJobDispatch , setCron} from "@/api/base/jobDispatch";
import { changeJobStatus } from "@/api/monitor/job";
import dispatchDialog from "@/views/components/device/dispatchDialog";
import settingThreshold from "@/views/components/device/settingThreshold";
export default {
  name: "jobDispatch",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      jobDispatchList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceNo: undefined,
      },
      // 表单参数
      form: {},
      // 列信息
      columns: [
        { key: 0, label: "任务名称", visible: true },
        { key: 1, label: "设备名称", visible: true },
        { key: 2, label: "设备编号", visible: true },
        { key: 3, label: "通道号", visible: true },
        { key: 4, label: "cron执行表达式", visible: true },
      ],
      dispatchVis: false, //设置周期的弹框
      ediExpressionObj: {}, //传给子组件的表达式的数据, 修改时候用
      cycleVis: false, //报警阈值的弹框
      cycleObj: {
        //阈值的设备和通道
        deviceNo: null,
        channel: null,
      },
    };
  },
  components: { dispatchDialog, settingThreshold },
  created() {
    this.getList();
  },
  methods: {
    /** 查询定时调度列表 */
    getList() {
      this.loading = true;
      listJobDispatch(this.queryParams).then((response) => {
        this.jobDispatchList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 设置周期 */
    setCycleFun(row) {
      if (row.job !== null && row.job !== undefined ) {
        //是单个修改
        this.ediExpressionObj = row;
        this.ediExpressionObj.cronExpression = row.job.cronExpression;
        this.ids = [row.id];
      } else {
        this.ediExpressionObj = {
          cronExpression: "0 0 0 1/1 * ?", //批量设置的默认值
          jobId: null,
        };
      }
      this.dispatchVis = true;
    },
    // 任务状态修改
    handleStatusChange(row) {
      let text = row.job.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.name + '"任务吗？').then(function() {
        return changeJobStatus(row.jobId, row.job.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.job.status = row.job.status === "0" ? "1" : "0";
      });
    },
    configDraFun(value) {
      //设置周期 , 请求接口
      let param = {
        ids: this.ids,
        cronExpression: value,
      };
      setCron(param).then((res) => {
        this.$modal.msgSuccess("设置成功");
        this.getList();
      });
    },
    cancelCrossBox() {
      //取消设置周期
      this.dispatchVis = false;
    },
    alarmCycleFun(e) {
      //点击报警阈值按钮
      this.cycleVis = true;
      this.cycleObj = {
        deviceNo: e.deviceNo,
        channel: e.channel,
      };
    },
    cancelCycleVis() {
      //取消报警阙值弹框
      this.cycleVis = false;
    },
  },
};
</script>
